#
#
# THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
# ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
# PARTICULAR PURPOSE.
#




#region Get-RegistryKey 

function Get-RegistryKey 
{        
    [Cmdletbinding()]
    Param(
        [Parameter(
			Position = 0,
			ValueFromPipeline = $true,
			ValueFromPipelineByPropertyName = $true,
			HelpMessage = "Hostname or IP address of the Relay to be queried."
			)] [Alias("h")]
		[string]$HostName = $Env:ComputerName,
        
		[Parameter(mandatory=$true)]
        [string]$Path,
				
        [Parameter()]
        [switch]$ReadWrite        
    )
    
    Begin 
    {
        Write-Verbose "[Get-RegistryKey] :: HostName = $HostName"
		Write-Verbose "[Get-RegistryKey] ::     Path = $Path"
    }
    
    Process 
    {        
        $Hive = [Microsoft.Win32.RegistryHive]"LocalMachine"
        Write-Verbose "[Get-RegistryKey] :: Hive = $Hive"
        
        $BaseKey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey($Hive,$HostName)
        Write-Verbose "[Get-RegistryKey] :: BaseKey = $BaseKey"
		
		try
        {
            $Key = $BaseKey.OpenSubKey("$Path",$ReadWrite)
            return $Key
        }
        catch
        {
            Write-Verbose "[Get-RegistryKey] ::  ERROR opening key:$Path"
        }
        
    }
}
    
#endregion 


#region Remove-RegistryKey 

function Remove-RegistryKey
{
    [Cmdletbinding(SupportsShouldProcess=$true)]
    Param(
    
		[Parameter(
			Position = 0,
			ValueFromPipeline = $true,
			ValueFromPipelineByPropertyName = $true,
			HelpMessage = "Hostname or IP address of the Relay to be queried."
			)] [Alias("h")]
		[string]$HostName = $Env:ComputerName,
        
        [Parameter(mandatory=$true)]
        [string]$Path,
        
        [Parameter(mandatory=$true)]
        [string]$Name,
        
        [Parameter()]
        [switch]$Recurse
    )

	Begin 
    {
		Write-Verbose "[Remove-RegistryKey] :: HostName = $HostName"
        Write-Verbose "[Remove-RegistryKey] :: Path = $Path"
    }
    
    Process 
    {
        if(Test-RegistryKey -Path $path\$name -ComputerName $HostName)
        {
            $Hive = [Microsoft.Win32.RegistryHive]"LocalMachine"            
            Write-Verbose "[Remove-RegistryKey] :: Hive = $Hive"
			
            $BaseKey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey($RegHive,$HostName)
            Write-Verbose "[Remove-RegistryKey] :: BaseKey = $BaseKey"
            
            $Key = $BaseKey.OpenSubKey($Path,$True)
            
            if($Recurse)
            {
                Write-Verbose "[Remove-RegistryKey] :: Calling DeleteSubKeyTree($Name)"
                $Key.DeleteSubKeyTree($Name)
            }
            else
            {
                Write-Verbose "[Remove-RegistryKey] :: Calling DeleteSubKey($Name)"
                $Key.DeleteSubKey($Name)
            }
        }
        else
        {
            "Key [$path\$name] does not exist"
        }
    }
}
    
#endregion 


#region Set-RegistryValue 

function Set-RegistryValue
{
    [Cmdletbinding(SupportsShouldProcess=$true)]
    Param(
		[Parameter(
			Position = 0,
			ValueFromPipeline = $true,
			ValueFromPipelineByPropertyName = $true,
			HelpMessage = "Hostname or IP address of the Relay to be queried."
			)] [Alias("h")]
		[string]$HostName = $Env:ComputerName,
        	
        [Parameter(mandatory=$true)]
        [string]$Path,
        
        [Parameter(mandatory=$true)]
        [string]$Name,
        
        [Parameter()]
        [string]$Value,
        
        [Parameter()]
        [string]$Type
        
    )
    
    Begin 
    {
        Write-Verbose "[Set-RegistryValue] :: Path = $Path"
        Write-Verbose "[Set-RegistryValue] :: Name = $Name"
        Write-Verbose "[Set-RegistryValue] :: Value = $Value"
        
        Switch ($Type)
        {
            "Unknown"       {$ValueType = [Microsoft.Win32.RegistryValueKind]::Unknown;continue}
            "String"        {$ValueType = [Microsoft.Win32.RegistryValueKind]::String;continue}
            "ExpandString"  {$ValueType = [Microsoft.Win32.RegistryValueKind]::ExpandString;continue}
            "Binary"        {$ValueType = [Microsoft.Win32.RegistryValueKind]::Binary;continue}
            "DWord"         {$ValueType = [Microsoft.Win32.RegistryValueKind]::DWord;continue}
            "MultiString"   {$ValueType = [Microsoft.Win32.RegistryValueKind]::MultiString;continue}
            "QWord"         {$ValueType = [Microsoft.Win32.RegistryValueKind]::QWord;continue}
            default         {$ValueType = [Microsoft.Win32.RegistryValueKind]::String;continue}
        }
        Write-Verbose "[Set-RegistryValue] :: Type = $Type"
    }
    
    Process 
    {
        Write-Verbose "[Set-RegistryValue] :: Get-RegistryKey -HostName $HostName -Path $path"
        $Key = Get-RegistryKey -HostName $HostName -Path $path
        Write-Verbose "[Set-RegistryValue] :: Get-RegistryKey returned $Key"
		
        Write-Verbose "[Set-RegistryValue] :: Setting Value for [$Name]"
        if($Value)
        {
            $Key.SetValue($Name,$Value,$ValueType)
        }
        else
        {
            $Key.SetValue($Name,$ValueType)
        }
        Write-Verbose "[Set-RegistryValue] :: Get-RegistryValue -HostName $HostName -Path $path -Name $Name"
        Get-RegistryValue -HostName $HostName -Path $path -Name $Name
    }
}
    
#endregion 


#region New-RegistryKey 

function New-RegistryKey
{
    [Cmdletbinding(SupportsShouldProcess=$true)]
    Param(
		[Parameter(
			Position = 0,
			ValueFromPipeline = $true,
			ValueFromPipelineByPropertyName = $true,
			HelpMessage = "Hostname or IP address of the Relay to be queried."
			)] [Alias("h")]
		[string]$HostName = $Env:ComputerName,
        	
        [Parameter(mandatory=$true)]
        [string]$Path,
        
        [Parameter(mandatory=$true)]
        [string]$Name        
    )
	
    Begin 
    {
        $ReadWrite = [Microsoft.Win32.RegistryKeyPermissionCheck]::ReadWriteSubTree
        
        Write-Verbose "[New-RegistryKey] :: HostName = $HostName"
		Write-Verbose "[New-RegistryKey] ::     Path = $Path"
    }

	Process 
    {        
        $Hive = [Microsoft.Win32.RegistryHive]"LocalMachine"        
        Write-Verbose "[New-RegistryKey] :: Hive = $Hive"
		
        $BaseKey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey($Hive,$HostName)
        Write-Verbose "[New-RegistryKey] :: BaseKey = $BaseKey"
		
        $Key = $BaseKey.OpenSubKey($Path,$True)
        $Key.CreateSubKey($Name,$ReadWrite)
    }
}
    
#endregion 


#region New-RegistryValue 

function New-RegistryValue
{
    [Cmdletbinding(SupportsShouldProcess=$true)]
    Param(
        [Parameter(
			Position = 0,
			ValueFromPipeline = $true,
			ValueFromPipelineByPropertyName = $true,
			HelpMessage = "Hostname or IP address of the Relay to be queried."
			)] [Alias("h")]
		[string]$HostName = $Env:ComputerName,
        	
        [Parameter(mandatory=$true)]
        [string]$Path,
        
        [Parameter(mandatory=$true)]
        [string]$Name,
        
        [Parameter()]
        [string]$Value,
        
        [Parameter()]
        [string]$Type
    )

	Begin 
    {
        Write-Verbose "[New-RegistryValue] :: Path = $Path"
        Write-Verbose "[New-RegistryValue] :: Name = $Name"
        Write-Verbose "[New-RegistryValue] :: Value = $Value"
        
        Switch ($Type)
        {
            "Unknown"       {$ValueType = [Microsoft.Win32.RegistryValueKind]::Unknown;continue}
            "String"        {$ValueType = [Microsoft.Win32.RegistryValueKind]::String;continue}
            "ExpandString"  {$ValueType = [Microsoft.Win32.RegistryValueKind]::ExpandString;continue}
            "Binary"        {$ValueType = [Microsoft.Win32.RegistryValueKind]::Binary;continue}
            "DWord"         {$ValueType = [Microsoft.Win32.RegistryValueKind]::DWord;continue}
            "MultiString"   {$ValueType = [Microsoft.Win32.RegistryValueKind]::MultiString;continue}
            "QWord"         {$ValueType = [Microsoft.Win32.RegistryValueKind]::QWord;continue}
            default         {$ValueType = [Microsoft.Win32.RegistryValueKind]::String;continue}
        }
        Write-Verbose "[New-RegistryValue] :: Type = $Type"
    }
    
    Process 
    {
        if(Test-RegistryValue -HostName $HostName -Path $path -Name $Name)
        {
            "Registry value already exist"     
        }
        else
        {
            Write-Verbose "[New-RegistryValue] :: Get-RegistryKey -HostName $HostName -Path $path"
			$Key = Get-RegistryKey -HostName $HostName -Path $path -ReadWrite
            Write-Verbose "[New-RegistryValue] :: Get-RegistryKey returned $Key"
			
            Write-Verbose "[New-RegistryValue] :: Setting Value for [$Name]"
            if($Value)
            {
                $Key.SetValue($Name,$Value,$ValueType)
            }
            else
            {
                $Key.SetValue($Name,$ValueType)
            }
            Write-Verbose "[New-RegistryValue] :: Get-RegistryValue -HostName $HostName -Path $path -Name $Name"
            Get-RegistryValue -HostName $HostName -Path $path -Name $Name

        }
    }
}
    
#endregion 


#region Test-RegistryValue 

function Test-RegistryValue
{
    [Cmdletbinding()]
    Param(
    	[Parameter(
			Position = 0,
			ValueFromPipeline = $true,
			ValueFromPipelineByPropertyName = $true,
			HelpMessage = "Hostname or IP address of the Relay to be queried."
			)] [Alias("h")]
		[string]$HostName = $Env:ComputerName,
        	
        [Parameter(mandatory=$true)]
        [string]$Path,
    
        [Parameter(mandatory=$true)]
        [string]$Name,
        
        [Parameter()]
        [string]$Value        
    )
    
    Process 
    {
        Write-Verbose "[Test-RegistryValue] :: Get-RegistryKey -HostName $HostName -Path $path"
        $Key = Get-RegistryKey -HostName $HostName -Path $path
        Write-Verbose "[Test-RegistryValue] :: Get-RegistryKey returned $Key"
        if($Value)
        {
            try
            {
                $CurrentValue = $Key.GetValue($Name)
                $Value -eq $CurrentValue
            }
            catch
            {
                $false
            }
        }
        else
        {
            try
            {
                $CurrentValue = $Key.GetValue($Name)
                if($CurrentValue){$True}else{$false}
            }
            catch
            {
                $false
            }
        }
    }
}
    
#endregion 

